package com.age;

import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class AgeCalculatorActivity extends Activity implements View.OnClickListener {
    /** Called when the activity is first created. */
	private static Spinner YearSpinner;
    private static Spinner MonthSpinner;
    private static Spinner DaySpinner;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        YearSpinner = (Spinner) findViewById(R.id.spinner1);
        MonthSpinner = (Spinner) findViewById(R.id.spinner2);
        DaySpinner = (Spinner) findViewById(R.id.spinner3);
        ArrayAdapter adapter1 = ArrayAdapter.createFromResource(this, R.array.Years, android.R.layout.simple_spinner_item);
        ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this, R.array.Years, android.R.layout.simple_spinner_item);
        ArrayAdapter adapter3 = ArrayAdapter.createFromResource(this, R.array.Years, android.R.layout.simple_spinner_item);
        YearSpinner.setAdapter(adapter1);
        MonthSpinner.setAdapter(adapter2);
        DaySpinner.setAdapter(adapter3);
        ((Button) findViewById(R.id.button1)).setOnClickListener(this);
    }
    public void onClick(View v) {
    	String year, month, day;
    	year = (String) YearSpinner.getSelectedItem();
    	month = (String) MonthSpinner.getSelectedItem();
    	day = (String) DaySpinner.getSelectedItem();
    	int yStart, mStart, dStart;
    	yStart=Integer.parseInt(year);
    	dStart=Integer.parseInt(year);
    	String[] monthNames = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    	mStart=0;
    	while (!monthNames[mStart].equals(month)) {
    		mStart++;
    	}
    	
    	Calendar rightNow = Calendar.getInstance();
    	int yNow = rightNow.YEAR;
    	int mNow = rightNow.MONTH;
    	int dNow = rightNow.DAY_OF_MONTH;
    	int numDays=0;
    	while (yNow!=yStart && mNow!=mStart && dNow!=dStart) {
    		dStart++;
    		if (newMonth(yStart, mStart, dStart)) {
    			dStart=1;
    			mStart++;
    		}
    		if (mStart>12) {
    			yStart++;
    			mStart=1;
    		}
    		numDays++;
    	}
    	double yAge, mAge, dAge;
    	yAge=
    	Toast.makeText(getApplicationContext(), toastStr, Toast.LENGTH_LONG).show();
    }
    private boolean newMonth(int year, int month, int day) {
    	int[] monthLengths = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    	int length=monthLengths[month-1];
    	if (month==2 && leapYear(year)) {
    		length=29;
    	}
    	if (day>length)
    		return true;
    	return false;
    }
    private boolean leapYear(int year) {
    	if (year%400==0)
    		return true;
    	if (year%100==0)
    		return false;
    	if (year%4==0)
    		return true;
    	return false;
    }
}